﻿using System;
namespace RainEngine.SceneManagement
{
    /// <summary>
    /// 保存索引集合。
    /// IndexList用来创建一个新List而无需创建一个新的集合对象。  
    /// 可以优化剔除过程
    /// </summary>
    class IndexList
    {
        public const int    LIST_CHUNK_SIZE = 1024 * 2;
        private int         size = LIST_CHUNK_SIZE;
        
        internal int[]      indices = new int[LIST_CHUNK_SIZE];
        internal int        counter = 0;     

        public void AddIndex(int index)
        {
            if (counter >= size)
                ResizeList();

            indices[counter++] = index;
        }

        public void Reset()
        {
            counter = 0;
        }

        /// <summary>
        /// 返回存储在集合中的索引数量
        /// </summary>
        public int Count { get { return counter; } }

        public int this[int index] { get { return indices[index]; } }

        private void ResizeList()
        {
            int[] tmp = new int[size + LIST_CHUNK_SIZE];
            Array.Copy(indices, tmp, size);
            indices = tmp;
            size += LIST_CHUNK_SIZE;
        }

        public void Sort(System.Collections.IComparer cmp)
        {
            if (counter <= 0)
                return;

            int[] tmp = new int[counter];
            Array.Copy(indices, tmp, counter);
            Array.Sort(tmp, cmp);
        }
    }
}
